﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
using System.Text;

namespace Xiaozhou.Utility
{
    public class Common
    {

        private readonly static Dictionary<string, string> typeDict = new Dictionary<string, string>();

        static Common()
        {
            typeDict.Add("int", "System.Int32");
            typeDict.Add("char", "System.Char");
            typeDict.Add("float", "System.Single");
            typeDict.Add("string", "System.String");
            typeDict.Add("double", "System.Double");
            typeDict.Add("byte", "System.Byte");
        }

        public static string getFileName(string inputPath)
        {
            return Path.GetFileNameWithoutExtension(inputPath);
        }

        public static string getConfigFileName(string inputPath)
        {
            return Path.Combine(Path.GetDirectoryName(inputPath),
                                getFileName(inputPath) + ".xml");
        }

        public static string getAssemblyFileName(string inputPath)
        {
            return Path.Combine(Path.GetDirectoryName(inputPath),
                    getFileName(inputPath) + ".dll");
        }

        public static bool ExistsConstructParams(string configFileName)
        {
            XElement data = XElement.Load(configFileName);
            return data.Element("ConstructParams").DescendantNodes().Count() > 0;
        }

        public static bool IsPluginEnabled(string configFileName)
        {

            XElement data = XElement.Load(configFileName);
            bool result;
            if (bool.TryParse(data.Element("EnablePlugin").Value, out result))
            {
                return result;
            }
            else
            {
                throw new Exception("Please check plugin enable status in config file:" + configFileName);
            }

        }

        public static Type[] GetParamTypes(string configFileName)
        {
            XElement data = XElement.Load(configFileName);
            var param = data.Element("ConstructParams").Elements("Param")
                            .Select(element => element.Attribute("type").Value);
            Type[] types = new Type[param.Count()];

            int index = 0;
            foreach (var type in param)
            {
                if (!typeDict.ContainsKey(type.ToLower()))
                {
                    return null;
                }

                types[index] = Type.GetType(typeDict[type.ToLower()]);
                index++;
            }

            return types;
        }

        public static Object[] GetParamValues(string configFileName)
        {
            XElement data = XElement.Load(configFileName);
            var param = data.Element("ConstructParams").Elements("Param")
                            .Select(element => element.Attribute("value").Value);
            Object[] objects = new Object[param.Count()];

            int index = 0;
            foreach (var type in param)
            {
                objects[index] = type;
                index++;
            }

            return objects;
        }

        public static string GetTypeName(string configFileName)
        {
            XElement data = XElement.Load(configFileName);
            var typeName = data.Element("TypeName").Value;
            return typeName.ToString();
        }

        public static bool ExistsReferenceList(string configFileName)
        {
            XElement data = XElement.Load(configFileName);
            return data.Element("ReferenceList").DescendantNodes().Count() > 0;
        }
    }
}
